package cn.oveay.classdesign.dao.impl;

import cn.oveay.classdesign.bean.basebean.Option;
import cn.oveay.classdesign.dao.OptionDao;
import cn.oveay.classdesign.util.jdbc.TxQueryRunner;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class OptionDaoImpl implements OptionDao {
    private QueryRunner qr = new TxQueryRunner();

    @Override
    public List<Option> findAll() {
        String sql = "select * from t_option";
        try {
            return qr.query(sql, new BeanListHandler<>(Option.class));
        } catch (SQLException e) {
            throw new RuntimeException("OptionDao Error.");
        }
    }

    @Override
    public List<Option> findBySubjectID(String subjectID) {
        String sql = "select * from t_option where subjectId=?";
        try {
            return qr.query(sql, new BeanListHandler<>(Option.class), subjectID);
        } catch (SQLException e) {
            throw new RuntimeException("OptionDao Error.");
        }
    }

    @Override
    public void inseart(Option option) {
        String sql = "insert into t_option values(?,?,?,?)";
        Object[] params = {option.getId(), option.getContent(), option.getIdx(), option.getSubjectId()};
        try {
            qr.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException("OptionDao Error." + e.getMessage());
        }
    }
}
